home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2001 / MacHack 2001.toast / pc / The Hacks / GrowBoxDock / Sources / ThreeDMetafile.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-30  |  13.9 KB  |  411 lines

  1. /*
  2.     File:        ThreeDMetafile.c
  3.  
  4.     Contains:    3D viewer support for SimpleText
  5.  
  6.     Version:    Mac OS X
  7.  
  8.     Disclaimer:    IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
  9.                 ("Apple") in consideration of your agreement to the following terms, and your
  10.                 use, installation, modification or redistribution of this Apple software
  11.                 constitutes acceptance of these terms.  If you do not agree with these terms,
  12.                 please do not use, install, modify or redistribute this Apple software.
  13.  
  14.                 In consideration of your agreement to abide by the following terms, and subject
  15.                 to these terms, Apple grants you a personal, non-exclusive license, under Apple’s
  16.                 copyrights in this original Apple software (the "Apple Software"), to use,
  17.                 reproduce, modify and redistribute the Apple Software, with or without
  18.                 modifications, in source and/or binary forms; provided that if you redistribute
  19.                 the Apple Software in its entirety and without modifications, you must retain
  20.                 this notice and the following text and disclaimers in all such redistributions of
  21.                 the Apple Software.  Neither the name, trademarks, service marks or logos of
  22.                 Apple Computer, Inc. may be used to endorse or promote products derived from the
  23.                 Apple Software without specific prior written permission from Apple.  Except as
  24.                 expressly stated in this notice, no other rights or licenses, express or implied,
  25.                 are granted by Apple herein, including but not limited to any patent rights that
  26.                 may be infringed by your derivative works or by other works in which the Apple
  27.                 Software may be incorporated.
  28.  
  29.                 The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
  30.                 WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
  31.                 WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  32.                 PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
  33.                 COMBINATION WITH YOUR PRODUCTS.
  34.  
  35.                 IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
  36.                 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  37.                 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  38.                 ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
  39.                 OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
  40.                 (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
  41.                 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42.  
  43.     Copyright © 1994-2001 Apple Computer, Inc., All Rights Reserved
  44. */
  45.  
  46. #include "MacIncludes.h"
  47. #include <String.h>
  48. #include "ThreeDMetafile.h"
  49.  
  50.  
  51. #if ALLOW_QUICKTIME
  52. static GlobalEntry    gProcPointers[] =
  53.     {
  54.     {uppViewerLibNewProcInfo, nil},
  55.     {uppViewerLibDisposeProcInfo, nil},
  56.  
  57.     {uppViewerLibUseFileProcInfo, nil},
  58.     {uppViewerLibUseDataProcInfo, nil},
  59.     
  60.     {uppViewerLibWriteFileProcInfo, nil},
  61.     {uppViewerLibWriteDataProcInfo, nil},
  62.     
  63.     {uppViewerLibDrawProcInfo, nil},
  64.  
  65.     {uppViewerLibGetPictProcInfo, nil},
  66.     {uppViewerLibGetButtonRectProcInfo, nil},
  67.  
  68.     {uppViewerLibGetDimensionProcInfo, nil},
  69.  
  70.     {uppViewerLibSetFlagsProcInfo, nil},
  71.     {uppViewerLibGetFlagsProcInfo, nil},
  72.  
  73.     {uppViewerLibSetBoundsProcInfo, nil},
  74.     {uppViewerLibGetBoundsProcInfo, nil},
  75.  
  76.     {uppViewerLibSetPortProcInfo, nil},
  77.     {uppViewerLibGetPortProcInfo, nil},
  78.  
  79.     {uppViewerLibEventProcInfo, nil},
  80.     {uppViewerLibAdjustCursorProcInfo, nil},
  81.  
  82.     {uppViewerLibGetStateProcInfo, nil},
  83.  
  84.     {uppViewerLibClearProcInfo, nil},
  85.     {uppViewerLibCutProcInfo, nil},
  86.     {uppViewerLibCopyProcInfo, nil},
  87.     {uppViewerLibPasteProcInfo, nil}
  88.     };    
  89. #endif
  90.     
  91.  
  92. // --------------------------------------------------------------------------------------------------------------
  93. // PRIVATE ROUTINES
  94. // --------------------------------------------------------------------------------------------------------------
  95. static UniversalProcPtr MakeARoutineDescriptor(void *theProc, ProcInfoType theProcInfo)
  96. /*
  97.     The Symantec compiler doesn't like jumping into staticly done routine descriptors,
  98.     so we have to actually allocate them as pointers.  Sigh.
  99. */
  100. {
  101.     RoutineDescriptor rd = BUILD_ROUTINE_DESCRIPTOR(0, nil);
  102.     
  103.     Ptr    newPtr;
  104.     
  105.     rd.routineRecords[0].procInfo = theProcInfo;
  106.     rd.routineRecords[0].procDescriptor = theProc;
  107.     rd.routineRecords[0].ISA = kPowerPCISA + kPowerPCRTA;
  108.     newPtr = NewPtrClear(sizeof(rd));
  109.     if (newPtr)
  110.         BlockMove(&rd, newPtr, sizeof(rd));
  111.     return((UniversalProcPtr) newPtr);
  112.     
  113. } // MakeARoutineDescriptor
  114.  
  115. // --------------------------------------------------------------------------------------------------------------
  116. static void FillInEntryPoints(void)
  117. {
  118. #if ALLOW_QUICKTIME
  119.     long                result;
  120.     
  121.     if ( 
  122.         (Gestalt('qd3d', &result) == noErr) && 
  123.         (Gestalt('sysa', &result) == noErr) && (result == 2) 
  124.         )
  125.         {
  126.         CFragSymbolClass    symClass;
  127.         CFragConnectionID    connID;
  128.         Ptr                 mainAddr;
  129.         Str255                errName;
  130.         long                procID = 0;
  131.         
  132.         if (GetSharedLibrary("\pQD3DViewerLib", 'pwpc', 1, &connID, &mainAddr, errName) == noErr)
  133.             {
  134.             FindSymbol(connID, "\pQ3ViewerNew", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  135.             FindSymbol(connID, "\pQ3ViewerDispose", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  136.             
  137.             FindSymbol(connID, "\pQ3ViewerUseFile", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  138.             FindSymbol(connID, "\pQ3ViewerUseData", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  139.  
  140.             FindSymbol(connID, "\pQ3ViewerWriteFile", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  141.             FindSymbol(connID, "\pQ3ViewerWriteData", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  142.  
  143.             FindSymbol(connID, "\pQ3ViewerDraw", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  144.  
  145.             FindSymbol(connID, "\pQ3ViewerGetPict", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  146.             FindSymbol(connID, "\pQ3ViewerGetButtonRect", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  147.  
  148.             FindSymbol(connID, "\pQ3ViewerGetDimension", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  149.  
  150.             FindSymbol(connID, "\pQ3ViewerSetFlags", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  151.             FindSymbol(connID, "\pQ3ViewerGetFlags", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  152.  
  153.             FindSymbol(connID, "\pQ3ViewerSetBounds", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  154.             FindSymbol(connID, "\pQ3ViewerGetBounds", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  155.  
  156.             FindSymbol(connID, "\pQ3ViewerSetPort", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  157.             FindSymbol(connID, "\pQ3ViewerGetPort", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  158.  
  159.             FindSymbol(connID, "\pQ3ViewerEvent", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  160.             FindSymbol(connID, "\pQ3ViewerAdjustCursor", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  161.  
  162.             FindSymbol(connID, "\pQ3ViewerGetState", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  163.  
  164.             FindSymbol(connID, "\pQ3ViewerClear", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  165.             FindSymbol(connID, "\pQ3ViewerCut", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  166.             FindSymbol(connID, "\pQ3ViewerCopy", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  167.             FindSymbol(connID, "\pQ3ViewerPaste", (Ptr*)&gProcPointers[procID++].entry, &symClass);
  168.             }
  169.         
  170.         // build the routine descriptors
  171.         {
  172.         short    i;
  173.         
  174.         for (i = 0; i < procID; ++i)
  175.             gProcPointers[i].entry = MakeARoutineDescriptor(gProcPointers[i].entry, gProcPointers[i].info);
  176.         }
  177.         
  178.         // as long as it isn't NIL, we're OK
  179.         if (gProcPointers[0].entry)
  180.             gMachineInfo.haveThreeD = true;
  181.         }
  182. #endif
  183. } // FillInEntryPoints
  184.  
  185. // --------------------------------------------------------------------------------------------------------------
  186. // OOP INTERFACE ROUTINES
  187. // --------------------------------------------------------------------------------------------------------------
  188.  
  189. static OSErr ThreeDCloseWindow(
  190.             WindowPtr pWindow, 
  191.             WindowDataPtr pData)
  192. {
  193. #pragma unused (pWindow)
  194.  
  195. #if ALLOW_QUICKTIME
  196.     ViewerLib_Dispose( ((ThreeDDataPtr)pData)->viewerObject);
  197. #endif
  198.  
  199.     return(noErr);
  200.     
  201. } // ThreeDCloseWindow
  202.  
  203. // --------------------------------------------------------------------------------------------------------------
  204. static OSErr ThreeDGetBalloon(
  205.             WindowPtr pWindow, 
  206.             WindowDataPtr pData, 
  207.             Point *localMouse,
  208.             short * returnedBalloonIndex, 
  209.             Rect *returnedRectangle)
  210. {
  211. #pragma unused (pWindow, pData, localMouse, returnedBalloonIndex, returnedRectangle)
  212.  
  213.     *returnedBalloonIndex = iDidTheBalloon;
  214.     
  215.     return(noErr);
  216.     
  217. } // ThreeDGetBalloon
  218.  
  219. // --------------------------------------------------------------------------------------------------------------
  220. static Boolean    ThreeDFilterEvent(
  221.             WindowPtr pWindow, 
  222.             WindowDataPtr pData, 
  223.             EventRecord *pEvent)
  224. {
  225. #pragma unused (pWindow)
  226.     
  227. #if ALLOW_QUICKTIME
  228.     if (pEvent->what == activateEvt)
  229.         {
  230.         if (pEvent->modifiers & activeFlag)
  231.             ViewerLib_SetFlags(((ThreeDDataPtr)pData)->viewerObject, ViewerLib_GetFlags(((ThreeDDataPtr)pData)->viewerObject) | kQ3ViewerActive);
  232.         else
  233.             ViewerLib_SetFlags(((ThreeDDataPtr)pData)->viewerObject, ViewerLib_GetFlags(((ThreeDDataPtr)pData)->viewerObject) & ~kQ3ViewerActive);
  234.         }
  235.         
  236.     return((Boolean)ViewerLib_Event (((ThreeDDataPtr)pData)->viewerObject, pEvent));
  237. #endif
  238.     return false;
  239.     
  240. } // ThreeDFilterEvent
  241.  
  242.  
  243. // --------------------------------------------------------------------------------------------------------------
  244. static OSErr ThreeDAdjustCursor(WindowPtr pWindow, WindowDataPtr pData, Point *localMouse, RgnHandle globalRgn)
  245. {
  246. #pragma unused (pWindow)
  247.  
  248.     OSErr anErr = noErr;
  249.  
  250. #if ALLOW_QUICKTIME
  251.     ViewerLib_AdjustCursor(((ThreeDDataPtr)pData)->viewerObject, localMouse);
  252.     
  253.     // since we don't know how the Viewer will adjust the cursor, make sure we get continuous calls
  254.     SetEmptyRgn(globalRgn);
  255. #endif
  256.     
  257.     return(eActionAlreadyHandled);
  258.     
  259. } // ThreeDAdjustCursor
  260.  
  261. // --------------------------------------------------------------------------------------------------------------
  262. static OSErr ThreeDGetCoachRectangle(WindowPtr pWindow, WindowDataPtr pData, Rect *pRect, Ptr name)
  263. {
  264. #pragma unused (pWindow)
  265.  
  266.     OSErr anErr = fnfErr;
  267.     
  268. #if ALLOW_QUICKTIME
  269.     if (strcmp(name, "Camera") == 0)
  270.         anErr = ViewerLib_GetButtonRect(((ThreeDDataPtr)pData)->viewerObject, kQ3ViewerButtonCamera, pRect);
  271.     if (strcmp(name, "Truck") == 0)
  272.         anErr = ViewerLib_GetButtonRect(((ThreeDDataPtr)pData)->viewerObject, kQ3ViewerButtonTruck, pRect);
  273.     if (strcmp(name, "Orbit") == 0)
  274.         anErr = ViewerLib_GetButtonRect(((ThreeDDataPtr)pData)->viewerObject, kQ3ViewerButtonOrbit, pRect);
  275.     if (strcmp(name, "Zoom") == 0)
  276.         anErr = ViewerLib_GetButtonRect(((ThreeDDataPtr)pData)->viewerObject, kQ3ViewerButtonZoom, pRect);
  277.     if (strcmp(name, "Dolly") == 0)
  278.         anErr = ViewerLib_GetButtonRect(((ThreeDDataPtr)pData)->viewerObject, kQ3ViewerButtonDolly, pRect);
  279. #endif
  280.             
  281.     return(anErr);
  282.     
  283. } // ThreeDGetCoachRectangle
  284.  
  285. // --------------------------------------------------------------------------------------------------------------
  286. void ThreeDGetFileTypes(
  287.         OSType * pFileTypes,
  288.         OSType * pDocumentTypes,
  289.         short * numTypes)
  290. {
  291. #if ALLOW_QUICKTIME
  292.     if (!gMachineInfo.haveThreeD)
  293.         FillInEntryPoints();
  294.         
  295.     if (gMachineInfo.haveThreeD)
  296.         {
  297.         pFileTypes[*numTypes]         = '3DMF';
  298.         pDocumentTypes[*numTypes]     = kThreeDWindow;
  299.         (*numTypes)++;
  300.         }
  301. #endif
  302. } // ThreeDGetFileTypes
  303.  
  304. // --------------------------------------------------------------------------------------------------------------
  305. static OSErr    ThreeDUpdateWindow(WindowPtr pWindow, WindowDataPtr pData)
  306. {
  307. #pragma unused (pWindow)
  308.  
  309. #if ALLOW_QUICKTIME
  310.     ViewerLib_Draw(((ThreeDDataPtr)pData)->viewerObject);
  311. #endif
  312.     
  313.     return(noErr);
  314.     
  315. } // ThreeDUpdateWindow
  316.  
  317. // --------------------------------------------------------------------------------------------------------------
  318. static OSErr    ThreeDAdjustMenus(WindowPtr pWindow, WindowDataPtr pData)
  319. {
  320. #pragma unused (pWindow)
  321.  
  322.     OSErr anErr = noErr;
  323.     
  324. #if ALLOW_QUICKTIME
  325.     if (ViewerLib_GetState(((ThreeDDataPtr)pData)->viewerObject) ) 
  326.         EnableCommand(cCopy);
  327. #endif
  328.  
  329.     return(anErr);
  330.     
  331. } // ThreeDAdjustMenus
  332.  
  333. // --------------------------------------------------------------------------------------------------------------
  334. static OSErr    ThreeDCommand(WindowPtr pWindow, WindowDataPtr pData, short commandID, long menuResult)
  335. {
  336. #pragma unused (menuResult)
  337.  
  338.     OSErr                        anErr = noErr;
  339. #if ALLOW_QUICKTIME
  340.     ViewerObject                 theViewer = ((ThreeDDataPtr)pData)->viewerObject;
  341.     
  342.     SetPortWindowPort (pWindow);
  343.  
  344.     switch (commandID) 
  345.         {
  346.         case cCopy:
  347.             anErr = ViewerLib_Copy(theViewer);
  348.             if (anErr == noErr)
  349.                 anErr = eActionAlreadyHandled;
  350.             break;
  351.         } // switch (commandID)
  352. #endif
  353.     return(anErr);
  354.     
  355. } // ThreeDCommand
  356.  
  357. // --------------------------------------------------------------------------------------------------------------
  358. static OSErr    ThreeDMakeWindow(
  359.             WindowPtr pWindow,
  360.             WindowDataPtr pData)
  361. {
  362.     OSErr                        anErr = noErr;
  363. #if ALLOW_QUICKTIME
  364.     ViewerObject                viewerObj;
  365.  
  366.     pData->pCloseWindow         = (CloseWindowProc)            ThreeDCloseWindow;
  367.     pData->pFilterEvent         = (FilterEventProc)            ThreeDFilterEvent;
  368.     pData->pGetBalloon             = (GetBalloonProc)            ThreeDGetBalloon;
  369.     pData->pUpdateWindow         = (UpdateWindowProc)        ThreeDUpdateWindow;
  370.     pData->pAdjustMenus         = (AdjustMenusProc)            ThreeDAdjustMenus;
  371.     pData->pCommand                 = (CommandProc)                ThreeDCommand;
  372.     pData->pAdjustCursor         = (AdjustCursorProc)        ThreeDAdjustCursor;
  373.     pData->pGetCoachRectangle    = (GetCoachRectangleProc)    ThreeDGetCoachRectangle;
  374.  
  375.     pData->dragWindowAligned    = 0;
  376.     
  377.     viewerObj = ViewerLib_New((CGrafPtr )pWindow,  &pData->contentRect,  kQ3ViewerDefault | kQ3ViewerDraggingOff); 
  378.  
  379.     if (viewerObj == 0)
  380.         {
  381.         anErr = memFullErr;
  382.         }
  383.     else
  384.         {
  385.         anErr = ViewerLib_UseFile(viewerObj, pData->dataRefNum);
  386.         if (anErr == noErr)
  387.             ((ThreeDDataPtr)pData)->viewerObject = viewerObj;
  388.         else
  389.             ViewerLib_Dispose(viewerObj);
  390.         
  391.         }
  392. #endif
  393.     
  394.     return(anErr);
  395.     
  396. } // ThreeDMakeWindow
  397.  
  398. // --------------------------------------------------------------------------------------------------------------
  399. OSErr    ThreeDPreflightWindow(PreflightPtr pPreflightData)
  400. {    
  401. #if ALLOW_QUICKTIME
  402.     pPreflightData->continueWithOpen     = true;
  403.     pPreflightData->makeProcPtr         = ThreeDMakeWindow;
  404.     pPreflightData->resourceID            = kThreeDWindowID;
  405.     pPreflightData->storageSize         = sizeof(ThreeDDataRecord);
  406. #endif
  407.  
  408.     return(noErr);
  409.     
  410. } // ThreeDPreflightWindow
  411.